package com.asp.aspchain.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.asp.aspchain.core.dao.DicBaseMapper;
import com.asp.aspchain.core.model.DicBaseModel;
import com.asp.aspchain.core.service.IDicBaseService;
import com.asp.aspchain.core.service.RedisService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * Service
 *
 * @author aspire
 * @Date 2018-10-24 11:22
 */
@Service
public class DicBaseServiceImpl extends ServiceImpl<DicBaseMapper, DicBaseModel> implements IDicBaseService {
	private static final Logger LOGGER = LoggerFactory.getLogger(DicBaseServiceImpl.class);
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private RedisService redisService;
	@Autowired
	private DicBaseMapper dicBaseMapper;


	/**
	 * 通过bkey获取配置数据(先查询缓存，缓存没数据再向数据库去查)
	 * 
	 * @param bkey
	 * @return TMitServiceDicBases
	 * @throws Exception
	 */
	@Override
	public String getBaseByBkey(String bkey) {
		String baseValue = null;
		Map<String, String> basesMap = (Map<String, String>) redisService.get("basesMap");
		LOGGER.info("获取到的缓存配置数据map" + basesMap);
		if (basesMap != null) {
			if (StringUtils.isNotBlank(basesMap.get(bkey))) {
				LOGGER.info("传入到的code" + bkey);
				baseValue = basesMap.get(bkey);
				LOGGER.info("通过code在缓存里获取到的数据" + baseValue);
			} else {
				baseValue=getValue(bkey);
			}
		} else {
			baseValue=getValue(bkey);

		}

		return baseValue;

	}

	/**
	 * 通过数据库获取配置数据(先查询缓存，缓存没数据再向数据库去查)
	 * 
	 * @param bkey
	 * @return String value
	 * @throws Exception
	 */
	public String getValue(String bkey) {
		String baseValue = null;
		DicBaseModel dicBaseModel = dicBaseMapper.selectById(bkey);
		if (dicBaseModel != null) {
			if (StringUtils.isNotBlank(dicBaseModel.getBvalue())) {
				baseValue = dicBaseModel.getBvalue().trim();
			} else {
				LOGGER.info("该配置信息不完善");
				return null;
			}

		} else {
			LOGGER.info("查询暂无该配置信息");
			return null;
		}
		return baseValue;
	}

	/**
	 * 获取配置数据
	 * 
	 * @param
	 * @return List<TMitServiceDicBases>
	 * @throws Exception
	 */

	@Override
	public Map<String, String> getBaseList() {
		Map<String, String> basesMap = (Map<String, String>) redisService.get("basesMap");
		LOGGER.info("获取到的缓存配置数据map" + basesMap);
		if (basesMap == null) {
			List<DicBaseModel> dicBaseModelList = dicBaseMapper.selectList(new EntityWrapper<DicBaseModel>().isNotNull("bkey"));
			LOGGER.info("通过数据库获取到的配置数据" + dicBaseModelList);
			if (dicBaseModelList != null && !dicBaseModelList.isEmpty()) {
				Map<String, String> map = new HashMap<String, String>();
				for (DicBaseModel base : dicBaseModelList) {
					map.put(base.getBkey(), base.getBvalue());
				}
				basesMap = map;
				redisService.set("basesMap", basesMap);
			}
		}
		return basesMap;
	}

}
